<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container disabled>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问卷名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入问卷名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问卷介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" autosize placeholder="请输入问卷介绍" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问卷链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input v-model="model.url" placeholder="请输入问卷链接"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问卷来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="source">
              <j-dict-select-tag type="list" v-model="model.source" dictCode="questionnaire_source" placeholder="请选择问卷来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="截止时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date :showTime="true"  date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择截止时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" autosize placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <sk-send-user v-model="sendUserJson" ></sk-send-user>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SkSendUser from "@/components/skComponent/skSendUser.vue";
  import { USER_INFO } from '@/store/mutation-types'
  import Vue from 'vue'
  export default {
    name: 'SkQuestionnaireSendForm',
    components: {
      SkSendUser
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        tenantId: Vue.ls.get(USER_INFO).relTenantIds,
        sendUserJson:{
          msgType:"",
          checkedKeys:[]
        },
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入名称!'},
           ],
           url: [
              { required: true, message: '请输入链接!'},
           ],
        },
        url: {
          // add: "/family/skQuestionnaireSend/add",
          add:"/family/skQuestionnaire/doReleaseQuestionnaire",
          edit: "/family/skQuestionnaireSend/edit",
          queryById: "/family/skQuestionnaireSend/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      TenantIds(){
        return this.$store.state.skCommon.relTenantIds.indexOf(Vue.ls.get(USER_INFO).relTenantIds) != -1
        // return this.$store.state.skCommon.relTenantIds == Vue.ls.get(USER_INFO).relTenantIds
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.sendUserJson.msgType = this.model.msgType;
        if(this.TenantIds){
          if (this.sendUserJson.msgType == "TENANT") {
            this.sendUserJson.tenantIds =  this.model.tenantIds
          }
          if (!this.sendUserJson.msgType) {
            this.sendUserJson.msgType = "ALLTENANT"
            this.sendUserJson.checkedKeys = []
          }
        }else{
          if(this.sendUserJson.msgType == "USER"){
            this.sendUserJson.checkedKeys =  this.model.userIds.split(",")
          }
          if (!this.sendUserJson.msgType || this.sendUserJson.msgType == "TENANT" || this.sendUserJson.msgType == "ALLTENANT") {
            this.sendUserJson.msgType = "PARENT"
            this.sendUserJson.checkedKeys = []
          }
        }
        console.log(this.sendUserJson);
      },
     
      submitForm () {
        this.model.msgType = this.sendUserJson.msgType;
        if(this.sendUserJson.msgType == "USER"){
          this.model.userIds =  this.sendUserJson.checkedKeys.join(",")
          if (this.model.userIds == "") {
            this.$message.warning("请选择指定用户！");
            return ;
          }
        }
        if (this.sendUserJson.msgType == "TENANT") {
          this.model.tenantIds =  this.sendUserJson.tenantIds
          if (!this.model.tenantIds) {
            this.$message.warning("请选择指定租户！");
            return ;
          }
        }
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            getAction(this.url.add,this.model).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>